<?php
/** Used by Rho_DB, no need to manually include this file.
*
* @see Rho_DB::factory()
* @package Rho
* @subpackage Rho-database */

/** Load the interface definition.*/
require_once Rho::$PATH.'rho/db/Rho_DB_Driver.inc.php';

/** mySQL driver.
* Do NOT use this class directly - use Rho_DB::factory()
*
* Uses the the old mysql functions.
*
* @see Rho_DB::factory()
* @package Rho
* @subpackage Rho-database */
class DB_mysql_old_Driver implements Rho_DB_Driver{

  private $_link;
  private $_name;

  /** @ignore */
  public function __construct(& $name){
    $this->_name='db.'.$name;
  }
  
  public function connect(){
    $this->_connect();
  }
  
  private function _connect(){
    if(isset($this->_link) ){
      return $this->_link; # already set so return
    }
    // connect to the DB server
    $server=Rho::conf($this->_name.'.server', 'localhost');
    if(Rho::conf($this->_name.'.use_perm') ){
      $this->_link=@mysql_connect($server, Rho::conf($this->_name.'.username'), Rho::conf($this->_name.'.password'), Rho::conf($this->_name.'.flags', 0) );
    }
    else {
      $this->_link=@mysql_connect($server, Rho::conf($this->_name.'.username'), Rho::conf($this->_name.'.password'), FALSE, Rho::conf($this->_name.'.flags', 0) );
    }
    // if not connected, throw exception
    if( ! $this->_link){
      throw new DB_Connection_Exception("DB ".$this->_name." Could not connect to $server: ".mysql_error(), mysql_errno());
      return FALSE;
    }
    // set encoding
    /* Note that mysql uses a different encoding naming convention
    * e.g. utf8 instead of UTF-8 */
    if(! @mysql_set_charset(Rho::conf($this->_name.'.enc', 'utf8'), $this->_link) ){
      Rho_Log::error(7100, 'DB mysql_old could not change client charset');
    }
    // select the database
    if(! @mysql_select_db(Rho::conf($this->_name.'.dbname'), $this->_link) ){      
      $e=new DB_Connection_Exception("DB Could not select database for ".$this->_name.': '.mysql_error($this->_link), mysql_errno($this->_link));
      $this->_link=FALSE;
      throw $e;
      return FALSE;
    }
    return $this->_link;
  }
  
  public function query(& $sql){
    if(! $this->_connect() ) return FALSE;
    $result=@mysql_query($sql);
    if(! $result){
      throw $this->getException('query failed');
      return FALSE;
    }
    return $result;
  }
  
  public function affectedRows(& $resource){
    if(! $this->_connect() ) return FALSE;
    $result=@mysql_affected_rows($this->_link);
    if($result==-1){
      throw $this->getException('last query failed - affectRows()');
      return FALSE;
    }
    return $result;
  }
  
  public function freeResult(& $resource){
    return @mysql_free_result($resource);
  }
  
  public function numRows(& $resource){
    $result=@mysql_num_rows($resource);
    if($result===FALSE){
      throw $this->getException('numRows returned false');
      return 0;
    }
    return $result;
  }
  
  private function getException($cause='Unknown'){
    if(isset($this->_link) && $this->_link!=FALSE){
      return new Rho_Exception('DB error: '.mysql_error($this->_link), 7000+mysql_errno($this->_link) );
    }
    return new Rho_Exception('DB error: '.$cause, 7000);
  }
  
  public function fetchObject(& $resource, & $record_class, & $model){
    return mysql_fetch_object($resource, $record_class, array($model));
  }
  
  public function seek(& $resource, $row_number){
    $result=@mysql_data_seek($resource, $row_number);
    if($result===FALSE){
      throw $this->getException('seek returned false');
      return FALSE;
    }
    return TRUE;
  }
  
  public function & getLink(){
    if(isset($this->_link) ){
      return $this->_link;
    }
    return NULL;
  }
  
  public function lastInsertID(& $resource){
    if(! $this->_connect() ) return FALSE;
    $result=@mysql_insert_id($this->_link);
    if(! $result){
      throw $this->getException('lastInsetID returned false');
      return 0;
    }
    return $result;
  }
  
  public function cleanString(& $str){
    stripslashes($str);    
  }
  
  public function convertDate(& $date){
    return;
  }
  
  public function convertTime(& $date){
    return;
  }
  
  public function convertDateTime(& $date){
    return;
  }
  
  public function escapeField(& $field, & $table){
    if(empty($table) ) return $field=='*' ? '*' : "`$field`" ;
    return $field=='*' ? $this->escapeTable($table).'*' : $this->escapeTable($table)."`$field`" ;
  }
  
  public function escapeTable(& $table){
    return "`$table`";
  }
  
  public function escapeString(& $str){
    if($str==NULL) return 'NULL';
    if(! $this->_connect() ){ // we need an active link 
      throw $this->getExcception('need connection to escapeString()');
      return "''";
    }
    return "'".mysql_real_escape_string($str, $this->_link)."'";
  }
  
  public function escapeDate(& $date){
    if($date==NULL) return 'NULL';
    return "'".$date."'";
  }
  
  public function escapeTime(& $time){
    if($time==NULL) return 'NULL';
    return "'".$time."'";
  }
  
  public function escapeDateTime(& $date){
    if($date==NULL) return 'NULL';
    return "'".$date."'";
  }
  
  public function limit($qty, $offset=NULL){
    return $offset==NULL ? "LIMIT $qty" : 'LIMIT  $offset,$qty';
  }
  
  public function close(){
    if(isset($this->_link) && $this->_link!=FALSE){
      mysql_close($this->_link);
      unset($this->_link);
    }
  }
  
  /** @ignore */
  function __destruct(){
    #echo ' mysql old driver dying';
    @$this->close();
  }
}